<template>
	<view class="mh100 bf">
		<view class="paytop f-c-c cf p-r" :style="{background:xzcolor}">
			<image class="p-a payimg b0 r0" :src="xzzfimg"></image>
			<!-- <view v-if="payObj.orderType" class="f30 mb10" style="opacity: 0.8;">{{payObj.info.storeName}}</view> -->
			<view class="paymoeny wei f60"><text class="">{{sl}}</text><text class="">{{amount}}</text></view>
			<block v-if=" discount && Number(discount)">
				<view>
					<text class="cf mr20" style="text-decoration: line-through;">￥{{originMoney}}</text>
					<text class="f24">（已优惠{{discount}}元）</text>
				</view>
			</block>
			<!-- <view v-if="payObj.orderType" class="p13 f26">{{payObj.info.type}}</view> -->
		</view>
		<form @submit="formSubmit">
			<view class="">
				<radio-group name='radiogroup' @change="radioChange">
					<label class="weui-cell right30 f-s" v-for='(v,i) in payArr' :key='i'>
						<view class="f-y-c">
							<image class='hdimg' mode='aspectFit' :src='v.img'></image>
						</view>
						<view class="weui-cell__bd">
							<view class="f-row">
								<view class="f34 f-1 l-n">{{v.name}}</view>
								<view class="weui-cell__ft_in-radio">
									<text class="iconfont f40" :class="v.value == form.payType?'icongx':'iconwgx'" :style="{color:v.value == form.payType?v.color||tColor:''}"></text>
									<radio :value="v.value" :checked="v.value == form.payType" class="weui-check" />
								</view>
							</view>
							<!-- <view class="mt20 mswz bf5 bs10 l-n f24 c9">{{v.text}}</view> -->
							<!-- 会员余额 -->
							<block v-if="form.payType == '3' && v.value == '3'">
								<u-form-item label="会员手机号:" label-width="160" :border-bottom='false'>
									<view class="flex">
										<u-input :custom-style="{fontSize:'28rpx'}" v-model="phoneNum" input-align="left" type="number" :clearable='false' placeholder="请输会员手机号" />
										<button class="f24" style="width: 120rpx;" :style="{ background: tColor }" @click="onUserSearch">搜索</button>
									</view>
								</u-form-item>
								<u-form-item label="用户信息:" label-width="160" v-if="userInfo.userTel">
									<view class="f-yc">
										<u-form-item label="用户ID:" label-width="160">
											{{userInfo.id}}
										</u-form-item>
										<u-form-item label="用户昵称:" label-width="160">
											{{userInfo.userName}}
										</u-form-item>
										<u-form-item label="用户余额:" label-width="160">
											￥{{userInfo.balance}}
										</u-form-item>
									</view>
								</u-form-item>
							</block>
							<!-- 支付宝 微信 刷卡 -->
							<block v-if="form.payType == '4' && v.value == '4'">
								<u-radio-group v-model="value">
									<view class="orderPay">
											<view v-for="(payModel,index) in payModels" :key="index">
												<view class="f-c-c" >
													<u-radio
														@change="changeModel" 
														:name="payModel.payModel"
														class="mt20"
													>
														{{payModel.name}}
													</u-radio>
												</view>
											</view>
									</view>
								</u-radio-group>
							</block>
							
						</view>
					</label>
				</radio-group>
			</view>
			<view v-if="payArr.length" class="foot-btnc">
				<button :disabled="loading" :loading="loading" form-type="submit" class="foot-btn b-s-2 bs0 f30 wei all-in" :style="{background:xzcolor}">确认支付</button>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data(){
			return{
				payArr: [],
				xzzfimg: '',
				xzcolor:"",
				
				phoneNum:'',
				tableId:'',
				discount:'',
				originMoney:'',
				amount:0,
				show:false,
				payModels:[
					{
						name:'微信',
						payModel:'1',
						src: 'cash_out.png'
						},
					{
						name:'支付宝',
						payModel:'2',
						src: 'cash_out.png'
						},
					{
						name:'刷卡',
						payModel:'3',
						src: 'cash_out.png'
						},
				],
				form:{
					payType:null, //2现金支付 3余额支付 4用户主动扫码支付
					payModel:null ,//1,2,3
					tableId:'',
					userId:'',
					money:null,
					userNote:''
				},
				userInfo:{}
			}
		},
		onLoad(option) {
			this.amount = option.amount
			
			this.discount = option.discount ?? ''
			this.originMoney = option.originMoney ?? ''
			this.form.tableId = option.tableId
			this.form.isOut = option.isOut || '1'
			this.form.userNote = option.userNote || ''
			this.getPayArr()
		},
		methods:{
			radioChange(e) {
				this.form.payType = e.detail.value
				let item = this.payArr.find(v => v.value == e.detail.value)
				this.util.setNB(item.color)
				this.xzcolor = item.color
				this.xzzfimg = item.img2
				// console.log(this.form.payType, item)
			},
			changeModel(e){
				this.form.payModel = e
			},
			getPayArr() {
				//可选支付类型
				let payArr = []
						payArr.push({
							name: '现金支付',
							value: '2',
							img: '/static/pay/wxzf.png',
							img2: '/static/pay/wxb.png',
							color: this.tColor,
							text: '更方便，更快捷'
						})
						payArr.push({
							name: '余额支付',
							value: '3',
							img: '/static/pay/yezf.png',
							img2: '/static/pay/yeb.png',
							color: this.tColor,
							text: '更方便，更快捷'
						})
						payArr.push({
							name: '其他方式',
							value: '4',
							img: '/static/pay/ttzf.png',
							img2: '/static/pay/ylbg.png',
							color: this.tColor,
							text: '更方便，更快捷'
						})
				this.payArr = payArr
				// let item = payArr.find(v => v.value != 'ye')
				this.radioChange({
					detail: {
						value: payArr[0].value
					}
				})
			},
			async formSubmit(e) {
				var messageDuration = 2000
				if(!this.form.payModel && this.form.payType == '4'){
					this.util.message('请选择支付方式', 3, messageDuration)
					return
				}
				// 以后可能有折扣需求 所以不在开始时就让amount 等于 amount
				this.form.money = this.amount
				try{
					let {msg} = await this.util.request({
						'url': this.api.pay,
						method:"post",
						data:this.form
					})
					this.util.message(msg, 3, messageDuration)
					var delta = 2
					if(this.form.tableId == '0'){
						delta = 3
					}
					setTimeout(()=>{
						uni.navigateBack({
							delta:delta
						})
					},messageDuration)
					
				}catch(e){
					this.util.message(e.msg, 3, messageDuration)
					//TODO handle the exception
				}
			},
			showModel(payType){
				this.show = true
			},
			async onUserSearch(){
				try{
					this.userInfo = {}
					let {code,data,msg} = await this.util.request({
						'url': this.api.getMember,
						data:{
							phone:this.phoneNum
						}
					})
					this.userInfo = {...data}
					this.userInfo.msg = msg
					this.form.userId = this.userInfo.id || ''
				}catch(e){
					this.util.message(e.msg, 3, 2000)
					//TODO handle the exception
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.paytop {
		padding: 10rpx 30rpx 30rpx;
	}
	.payimg {
		width: 145rpx;
		height: 125rpx;
	}
	.paymoeny {
		display: flex;
		align-items: baseline;
	}
	.weui-cell {
		padding: 45rpx 35rpx 30rpx;
	}
	
	.weui-cell:last-child:after {
		display: block;
	}
	
	.weui-cell:after {
		left: 112rpx;
	}
	.right30::after {
		right: 30rpx;
	}
	.hdimg {
		width: 49rpx;
		height: 49rpx;
		margin-right: 30rpx;
	}
	.mswz {
		padding: 10rpx 13rpx;
	}
	.orderPay{
		padding: 20rpx 0;
		display: flex;
		justify-content: space-around;
	}
	::v-deep .orderPay .u-radio__label{
		margin-right: 0;
	}
	.all-in{
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		border-radius: 80rpx;
	}
</style>
